<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false" width="70%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-row :gutter="20">
            <el-col :xs="24" :lg="colConfig">
          <el-form-item label="编码" prop="code">
     <el-input v-model="dataForm.code" placeholder="编码"></el-input>
</el-form-item>
                <el-form-item label="名称" prop="name">
     <el-input v-model="dataForm.name" placeholder="名称"></el-input>
</el-form-item>
                <el-form-item label="业务类型" prop="bussnessType">
     <el-input v-model="dataForm.bussnessType" placeholder="业务类型"></el-input>
</el-form-item>
                <el-form-item label="变更原因" prop="changeType">
     <el-input v-model="dataForm.changeType" placeholder="变更原因"></el-input>
</el-form-item>
                <el-form-item label="变更状态" prop="changeStatus">
     <el-input v-model="dataForm.changeStatus" placeholder="变更状态"></el-input>
</el-form-item>
                <el-form-item label="业务表id" prop="tableId">
     <el-input v-model="dataForm.tableId" placeholder="业务表id"></el-input>
</el-form-item>
                <el-form-item label="业务表编码" prop="tableCode">
     <el-input v-model="dataForm.tableCode" placeholder="业务表编码"></el-input>
</el-form-item>
                <el-form-item label="业务表名称" prop="tableName">
     <el-input v-model="dataForm.tableName" placeholder="业务表名称"></el-input>
</el-form-item>
                <el-form-item label="变更说明" prop="description">
     <el-input v-model="dataForm.description" placeholder="变更说明"></el-input>
</el-form-item>
                <el-form-item label="变更人" prop="bguserName">
     <el-input v-model="dataForm.bguserName" placeholder="变更人"></el-input>
</el-form-item>
                  <el-form-item label="状态" prop="status" size="mini">
   <el-radio-group v-model="dataForm.status">
       <el-radio :label="0">停用</el-radio>
       <el-radio :label="1">正常</el-radio>
   </el-radio-group>
</el-form-item>
                        <el-form-item label="审核ID" prop="auditId">
     <el-input v-model="dataForm.auditId" placeholder="审核ID"></el-input>
</el-form-item>
                    </el-col>
        </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      colConfig: 1, // 列配置 1 2列
      visible: false,
      dataForm: {
        id: '',
        code: '',
        name: '',
        bussnessType: '',
        changeType: '',
        changeStatus: '',
        tableId: '',
        tableCode: '',
        tableName: '',
        description: '',
        bguserName: '',
        deptId: '',
        status: 1,
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        auditId: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        code: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        bussnessType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        changeType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        changeStatus: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tableId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tableCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tableName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        description: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        bguserName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        auditId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.colConfig = 24 / this.colConfig
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/business/bsbussnesschange/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/business/bsbussnesschange/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
